/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.metadata.rtcustomization.servermanager.repository;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.lcm.metadata.api.entity.MetadataPackage;
import com.inspur.edp.lcm.metadata.common.ServiceUtils;
import com.inspur.edp.lcm.metadata.common.FileServiceImp;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.*;

@Slf4j
public class MetadataPackageRepoService {

    public Map<String, MetadataPackage> getMetadataPackagesRecursivly(String path) {
        // 获取目录下所有的元数据包文件
        ServiceUtils.checkEmpty(path, "path不可为空");
        List<File> mdpkgs = FileServiceImp.listAllFiles(new File(path), pathname -> pathname.getPath().endsWith(ServiceUtils.getMetadataPackageExtension()));

        // 解析元数据包
        if (mdpkgs == null || mdpkgs.size() == 0) {
            return null;
        }
        Map<String, MetadataPackage> mdpkgMap = new HashMap<>();
        ObjectMapper mapper = ServiceUtils.getMapper();
        for (File mdpkg : mdpkgs) {
            Map<String, String> manifestMap = ServiceUtils.readCompressedFile(mdpkg, ServiceUtils.getManifestFileName());
            MetadataPackage metadataPackage;
            try {
                metadataPackage = mapper.readValue(manifestMap.get(ServiceUtils.getManifestFileName()), MetadataPackage.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException("无法解析元数据包中的manifest文件：" + mdpkg.getAbsolutePath());
            }
            mdpkgMap.put(mdpkg.getAbsolutePath(), metadataPackage);
        }
        return mdpkgMap;
    }

//    public HashMap<String, MetadataPackage> retrieveAllMdPkg(String path) throws IOException {
//        HashMap<String, MetadataPackage> packageDic = new HashMap<>();
//        retrieveMdPkgRecursively(path, packageDic);
//        return packageDic;
//    }
//
//    public void retrieveMdPkgRecursively(String path, HashMap<String, MetadataPackage> packageDic) throws IOException {
//        if (path.isEmpty() || new File(path).getName().toLowerCase().equals("web")) {
//            return;
//        }
//        List<File> files = fileService.getAllFiles(path);
//        if (files.size() >0) {
//            List<File> fileList = new ArrayList<>();
//            for (File file : files) {
//                if (fileService.getExtension(file.toString()).equals(MetadataUtils.getMetadataPackageExtension())) {
//                    fileList.add(file);
//                }
//            }
//            if (fileList !=null && fileList.size()>0) {
//                for (File file1 : fileList) {
//                    try{
//                        MetadataPackage metadataPackage = getMetadataPackage(file1);
//                        packageDic.put(Utils.handlePath(file1.toString()),metadataPackage);
//                    }
//                    catch (IOException e) {
//                        e.printStackTrace();
//                        System.out.println(e.getMessage() + ":" + file1.getAbsolutePath());
//                        log.error(e.getMessage() + ":" + file1.getAbsolutePath());
//                    }
//                }
//            }
//        }
//        // 路径中目录
//        List<File>  dirs = fileService.getDirectorys(path);
//        for (int i = 0; i < dirs.size(); i++)
//        {
//            retrieveMdPkgRecursively(dirs.get(i).toString(), packageDic);
//        }
//    }
//
//    @Override
//    public MetadataPackage getMetadataPackage(File packageFile) throws IOException {
//        String manifestStr = "";
//        MetadataPackage metadataPackage;
//        StringBuilder sb = new StringBuilder();
//        InputStream in = new BufferedInputStream(new FileInputStream(packageFile));
//        ZipFile zf = new ZipFile(packageFile);
//        Charset utf8 = Charset.forName("UTF-8");
//        ZipInputStream zipInputStream = new ZipInputStream(in, utf8);
//        ZipEntry zipEntry;
//        try {
//            while((zipEntry = zipInputStream.getNextEntry()) != null){
//                BufferedReader br = new BufferedReader(
//                        new InputStreamReader(zf.getInputStream(zipEntry),"UTF-8"));
//                if(zipEntry.toString().equals(MetadataUtils.getManifestFileName())){
//                    String line;
//                    while((line = br.readLine()) != null){
//                        sb.append(line);
//                    }
//                    manifestStr = sb.toString();
//                }
//                br.close();
//            }
//        } catch (IOException e) {
//            log.error("Error in reading mdpkg, Package is " + packageFile.getAbsolutePath(),e);
//            throw new RuntimeException("Error in reading mdpkg, Package is " + packageFile.getAbsolutePath(),e);
//        }finally {
//            zipInputStream.close();
//            zf.close();
//            in.close();
//        }
////                        String jsonString = handleJsonString(manifestStr);//TODO可能会引起序列化失败
//        try {
//            metadataPackage = mapper.readValue(manifestStr,MetadataPackage.class);
//            log.debug("元数据包：" + packageFile + "已加载。");
//        } catch (Exception e) {
//            log.error("元数据包中manifest.json反序列化失败，元数据包为"+ packageFile,e);
//            throw new RuntimeException("元数据包中manifest.json反序列化失败，元数据包为"+ packageFile,e);
//        }
//        return metadataPackage;
//    }
//
//    @Override
//    public void getManifestFilesPath(String originalPath, List<String> manifestPath) {
//
//    }
//
//    @Override
//    public void getMdpkgFilesPath(String originalPath, List<String> mdpkgPath) {
//        File dir = new File(originalPath);
//        List<File> paths = fileService.getAllFiles(originalPath);
//        if (paths.size() > 0) {
//            paths.forEach(path->{
//                String extension = fileService.getExtension(path);
//                if (extension.equals(MetadataUtils.getMetadataPackageExtension())) {
//                    mdpkgPath.add(handlePath(path.toString()));
//                }
//            });
//            searchChildPath4Mdpkg(originalPath, mdpkgPath, dir);
//        }else {
//            searchChildPath4Mdpkg(originalPath,mdpkgPath,dir);
//        }
//    }
//
//    private void searchChildPath4Mdpkg(String originalPath, List<String> mdpkgPath, File dir) {
//        List<File> dirs = fileService.getDirectorys(dir.toString());
//        if (dirs.size() > 0) {
//            for (int i = 0; i < dirs.size(); i++) {
//                String temPath = originalPath + "/" + fileService.getFileName(handlePath(dirs.get(i).toString()));
//                getMdpkgFilesPath(temPath,mdpkgPath);
//            }
//        }
//    }
//
//    private String handlePath(String path)
//    {
//        return path.replace("\\", "/");
//    }
}
